package top.knos.od.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import top.knos.od.common.PageVo;
import top.knos.od.common.Result;
import top.knos.od.exception.TipsException;
import top.knos.od.service.FeeItemService;
import top.knos.od.utils.JsonUtil;
import top.knos.od.vo.DeleteStringIdReq;
import top.knos.od.vo.FeeItemReq;
import top.knos.od.vo.FeeItemVo;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/api/feeItem")
public class FeeItemController {
    @Autowired
    private FeeItemService feeItemService;

    /**
     * 新增
     * @param feeItemVo
     * @return
     */
    @PostMapping(value = "add")
    @ResponseBody
    @Transactional(rollbackFor = TipsException.class)
    public Result<Object> addFeeItem(@RequestBody FeeItemVo feeItemVo) {
        feeItemService.save(feeItemVo);
        return Result.ok();
    }

    /**
     * 删除
     *
     * @param deleteReq
     * @return
     */
    @RequestMapping(value = "delete")
    @Transactional(rollbackFor = TipsException.class)
    public Result<Object> delete(@RequestBody DeleteStringIdReq deleteReq) {
        feeItemService.delete(deleteReq);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping(value = "update")
    @ResponseBody
    @Transactional(rollbackFor = TipsException.class)
    public Result<Object> updateFeeItem(@RequestBody FeeItemVo feeItemVo) {
        feeItemService.update(feeItemVo);
        return Result.ok();
    }

    /**
     * 分页请求
     *
     * @param feeItemReq 分页请求
     * @return
     */
    @PostMapping(value = "query")
    public Result<PageVo<FeeItemVo>> query(@RequestBody FeeItemReq feeItemReq)  throws Exception {
        return Result.ok(feeItemService.query(feeItemReq));
    }

    @GetMapping("export")
    public void exportState(@RequestParam String params,
                            HttpServletResponse response) throws Exception {
        FeeItemReq req  = JsonUtil.parseObject(params,FeeItemReq.class);
        req.setCurrent(1);
        req.setPageSize(Integer.MAX_VALUE);
        PageVo<FeeItemVo> pageVo = feeItemService.query(req);

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("费用项", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), FeeItemVo.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("费用项明细").doWrite(pageVo.getData());
    }
}
